package org.jsets.fastboot.concurrent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 可命名的线程工厂
 * 
 * @author wangjie (https://github.com/wj596)
 * @date 2022.01.11 23:56
 * 
 */
@Slf4j
public class NamedThreadFactory implements ThreadFactory {

	private final String prefix;
	private final AtomicInteger count = new AtomicInteger(1);

	public NamedThreadFactory(String prefix) {
		this.prefix = prefix;
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread thread = new Thread(r);
		String name = prefix + "_" + count.getAndIncrement();
		thread.setName(name);
		log.info("create thread name[{}]", name);
		return thread;
	}

}